﻿using System.Data;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using StoreAPI.Models;
using StoreAPI.Services;
using StoreAPI.Services.ServerImpls;

namespace StoreAPI.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class OutboundRecordController : ControllerBase
    {
        private readonly IOutboundRecordService _outboundRecordService;
public OutboundRecordController(IOutboundRecordService outboundRecordService)
        {
            _outboundRecordService = outboundRecordService;
        }
        //得到最大ID
        [HttpPost("GetMaxId")]
        public ActionResult GetMaxId()
        {
            var maxId = _outboundRecordService.GetMaxId();
            if (maxId != 0)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "获取成功",
                    MaxId = maxId
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "获取失败"
            });
        }
        //是否存在该记录
        [HttpPost("IsExist")]
        public ActionResult Exists(int OutboundId)
        {
            if (_outboundRecordService.Exists(OutboundId))
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "存在"
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "不存在"
            });
        }

        //添加
        [HttpPost("AddOutboundRecord")]
        public ActionResult Add([FromBody] OutboundRecord model)
        {
            if (_outboundRecordService.Add(model))
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "添加成功"
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "添加失败"
            });
        }

        //更新
        [HttpPost("UpdateOutboundRecord")]
        public ActionResult Update([FromBody] OutboundRecord model)
        {
            if (_outboundRecordService.Update(model))
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "更新成功"
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "更新失败"
            });
        }
        //删除
        [HttpPost("DeleteOutboundRecord")]
        public ActionResult Delete([FromBody] int inventoryId)
        {
            if (_outboundRecordService.Delete(inventoryId))
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "删除成功"
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "删除失败"
            });
        }
        //批量删除数据
        [HttpPost("DeleteOutboundRecords")]
        public ActionResult DeleteOutboundRecords([FromBody] int[] inventoryIds)
        {
            if (_outboundRecordService.DeleteList(inventoryIds))
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "删除成功"
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "删除失败"
            });
        }
        //得到一个对象实体
        [HttpPost("GetOutboundRecord")]
        public ActionResult GetUser([FromBody] int OutboundId)
        {
            var outboundRecord = _outboundRecordService.GetModel(OutboundId);
            if (outboundRecord != null)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "获取成功",
                    Data = outboundRecord
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "获取失败"
            });
        }
        //查询一行数据
        [HttpPost("DataRowToOutboundRecord")]
        public ActionResult DataRowToOutboundRecord([FromBody] DataRow dr)
        {
            var outboundRecord = _outboundRecordService.DataRowToModel(dr);
            if (outboundRecord != null)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "转换成功",
                    Data = outboundRecord
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "转换失败"
            });
        }


        //获取全部用户
        [HttpPost("GetListAllOutboundRecord")]
        public ActionResult GetListAllOutboundRecord([FromBody] Expressionable<OutboundRecord> exp)
        {
            var outboundRecord = _outboundRecordService.GetListAll(exp);
            if (outboundRecord != null)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "获取成功",
                    Data = outboundRecord
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "获取失败"
            });
        }
        //获得前几行数据
        [HttpPost("GetListOutboundRecord")]
        public ActionResult GetListUser([FromBody] Expressionable<OutboundRecord> exp, int Top)
        {
            var outboundRecord = _outboundRecordService.GetList(exp, Top);
            if (outboundRecord != null)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "获取成功",
                    Data = outboundRecord
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "获取失败"
            });
        }
        //获取记录总数
        [HttpPost("GetRecordCountOutboundRecord")]
        public ActionResult GetRecordCountUser([FromBody] Expressionable<OutboundRecord> exp)
        {
            if (_outboundRecordService.GetRecordCount(exp) > 0)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "获取成功",
                    Count = _outboundRecordService.GetRecordCount(exp)
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "获取失败"
            });

        }


        //分页
        [HttpPost("GetOutboundRecordPage")]
        public ActionResult GetUserPage([FromBody] Expressionable<OutboundRecord> exp, int pageIndex = 1, int pageSize = 10)
        {
            var totalPage = 0;
            var outboundRecord = _outboundRecordService.GetListByPage(exp, out totalPage, pageIndex, pageSize);
            if (outboundRecord != null)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "获取成功",
                    Data = outboundRecord,
                    TotalPage = totalPage
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "获取失败"
            });
        }
    }
}
